package com.enqualcomm.kids.activities;

import android.widget.TextView;

import com.enqualcomm.kids.R;
import com.enqualcomm.kids.base.BaseActivity;
import com.enqualcomm.kids.config.userdefault.AppDefault;
import com.enqualcomm.kids.config.userdefault.UserDefault;
import com.enqualcomm.kids.view.gesturelock.GestureLockView;

import org.androidannotations.annotations.AfterViews;
import org.androidannotations.annotations.EActivity;
import org.androidannotations.annotations.ViewById;

import common.utils.PromptUtil;

/**
 * Created by Sunzhigang on 2016/3/21.
 */
@EActivity(R.layout.activity_gesturelock)
public class GestureLockActivity extends BaseActivity {
    private UserDefault userDefault;

    private String firstInputKey;
    private int inputCount = 0;

    @ViewById(R.id.gesture_glv)
    GestureLockView gesture_glv;
    @ViewById(R.id.message_tv)
    TextView message_tv;

    @AfterViews
    void setupViews() {
        userDefault = new UserDefault(new AppDefault().getUserid());
        initGestureView();
        message_tv.setText("绘制解锁图案");
    }

    private void initGestureView() {
        gesture_glv.setOnGestureFinishListener(new GestureLockView.OnGestureFinishListener() {
            @Override
            public void OnGestureFinish(boolean success, String key) {
                if (!key.isEmpty()) {
                    setInputKey(key);
                }
            }
        });
    }

    private void setInputKey(String key) {
        if (inputCount == 0) {
            firstInputKey = key;
            gesture_glv.setResult(true);
            message_tv.setText("确认解锁图案");
        } else if (firstInputKey.equals(key)) {
            gesture_glv.setResult(true);

            userDefault.saveGestureLock(key);
            userDefault.setStartOpenGestureLock(true);
            userDefault.setGestureLock();

            PromptUtil.longToast(this, "绘制图形锁成功");
            startActivity(GestureLockControlActivity_.class);
            finish();
        } else {
            gesture_glv.setResult(false);
            PromptUtil.longToast(this, "与上一次绘制图案不一样");
        }
        inputCount++;
    }
}
